home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / mpeg / main.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  255 lines

  1. /**********************************************************************
  2.  *. This file is part of BIT shareware package.
  3.  *
  4.  *  Copyright(c) 1993, 1994 by T.C. Zhao
  5.  *  All rights reserved.
  6.  *.
  7.  *
  8.  **********************************************************************/
  9.  
  10. #include "video.h"
  11. #include "proto.h"
  12. #include <sys/types.h>
  13. #include <signal.h>
  14. #include <netinet/in.h>
  15. #include <stdlib.h>
  16.  
  17. #include "util.h"
  18. #include "dither.h"
  19.  
  20. #define BUF_LENGTH 80000    /* Define buffer length. */
  21.  
  22. double realTimeStart;
  23. int totNumFrames = 0;
  24. FILE *input;            /* Global file pointer to incoming data. */
  25. int EOF_flag = 0;
  26. int loopFlag;            /* Loop flag. */
  27. int quietFlag;            /* Quiet flag. */
  28. int noDisplayFlag;        /* Display image on screen? */
  29. int run_in_background;
  30. int seek_only;
  31. jmp_buf env;            /* Setjmp/Longjmp env. */
  32. jmp_buf errjmp;
  33. int ditherType = FULL_COLOR_DITHER;
  34.  
  35.  
  36. void
  37. frame_re_init(void)
  38. {
  39.     EOF_flag = 0;
  40.     curBits = 0;
  41.     bitOffset = 0;
  42.     bufLength = 0;
  43.     bitBuffer = NULL;
  44.     totNumFrames = 0;
  45. }
  46.  
  47. /***** Initialize the entire decoder system *****************/
  48.  
  49. void
  50. mpeg_system_init(void)
  51. {
  52.     static mpegsysinit;
  53.  
  54.     if (!mpegsysinit)
  55.       {
  56.  
  57.       LUM_RANGE = 8;
  58.       CR_RANGE = CB_RANGE = 4;
  59.  
  60.       lum_values = (int *) malloc(LUM_RANGE * sizeof(int));
  61.       cr_values = (int *) malloc(CR_RANGE * sizeof(int));
  62.       cb_values = (int *) malloc(CB_RANGE * sizeof(int));
  63.       ditherType = FULL_COLOR_DITHER;
  64.  
  65.       init_tables();
  66.  
  67.       InitColorDither();
  68.       }
  69.     mpegsysinit = 1;
  70. }
  71.  
  72. void
  73. set_video_stream(FILE * fp)
  74. {
  75.     input = fp;
  76. }
  77.  
  78. static VidStream *theStream;
  79. extern void glDisplay(VidStream *);
  80.  
  81. static char *parse_command_line(int, char **argv);
  82.  
  83. /***********************************************************************
  84.  * Main
  85.  *  Call mpegVidRsrc to get next frame and glDisplay to
  86.  *  display the frame.
  87.  **********************************************************************/
  88. int
  89. main(int argc, char **argv)
  90. {
  91.     char *name;
  92.  
  93.     if ((name = parse_command_line(argc, argv)))
  94.     input = fopen(name, "r");
  95.  
  96.  
  97.     if (!input)
  98.       {
  99.       fprintf(stderr, "bad file\n");
  100.       exit(1);
  101.       }
  102.  
  103.     /* set up error handler */
  104.  
  105.     if (setjmp(errjmp))
  106.       {
  107.       fprintf(stderr, "Can't continue");
  108.       exit(0);
  109.       }
  110.  
  111.     do
  112.       {
  113.  
  114.       theStream = NewVidStream(BUF_LENGTH);
  115.       mpeg_system_init();
  116.  
  117.       if (!(mpegVidRsrc(0, theStream)))
  118.           continue;
  119.  
  120.  
  121.       realTimeStart = ReadSysClock();
  122.  
  123.       while (mpegVidRsrc(0, theStream))
  124.         {
  125.         totNumFrames++;
  126.         if (!quietFlag)
  127.             fprintf(stderr, "%d\r", totNumFrames);
  128.         glDisplay(theStream);
  129.         }
  130.  
  131.       PrintTimeInfo();
  132.  
  133.       if (loopFlag)
  134.         {
  135.         DestroyVidStream(theStream);
  136.         theStream = 0;
  137.         rewind(input);
  138.         reset_mpeg_stream();
  139.         frame_re_init();
  140.         }
  141.       }
  142.     while (loopFlag);
  143.  
  144.     return 0;
  145. }
  146.  
  147. /******************************************************************
  148.  * Handle command line
  149.  *****************************************************************/
  150. static char *options = "[-nlhbq -m f]";
  151. static char *optstr = "nlhbqm:";
  152. static char *helps[] =
  153. {
  154.     "\t-h: for this help", "\t-l: loop back",
  155.     "\t-n: no display", "\t-b: run in background",
  156.     "\t-q: quiet. No timing stat",
  157. #if 0
  158.     "\t-d i: frame delay. In milli-seconds\n",
  159. #endif
  160.     "\t-m f: magnigication",
  161.     "   Use SPACE key to pause",
  162.     "   Use RIGHTMOUSE for pop-up menu",
  163.     0
  164. };
  165.  
  166.  
  167. static void
  168. usage(char *cmd, int die, int v)
  169. {
  170.     char **p = helps;
  171.  
  172.     fprintf(stderr, "%s: Part of BIT package\n", cmd);
  173.     fprintf(stderr, "Usage: %s %s mpegfile\n", cmd, options);
  174.  
  175.     if (v)
  176.       {
  177.       while (*p)
  178.         {
  179.         fprintf(stderr, "%s\n", *p);
  180.         p++;
  181.         }
  182.       }
  183.  
  184.     if (die)
  185.     exit(1);
  186. }
  187.  
  188. extern char *optarg;
  189. extern int optind, opterr;
  190. static char *
  191. parse_command_line(int argc, char **argv)
  192. {
  193.     int c;
  194.     float mf;
  195.  
  196.     opterr = 0;
  197.  
  198.     if (argc < 2)
  199.     usage(*argv, 1, 0);
  200.  
  201.     while ((c = getopt(argc, argv, optstr)) != EOF)
  202.       {
  203.       switch (c)
  204.         {
  205.         case 'h':
  206.         usage(argv[0], 1, 1);
  207.         break;
  208.         case 'l':
  209.         loopFlag = 1;
  210.         break;
  211.         case 'n':
  212.         noDisplayFlag = 1;
  213.         break;
  214.         case 'b':
  215.         run_in_background = 1;
  216.         break;
  217.         case 'q':
  218.         quietFlag = 1;
  219.         break;
  220.         case 'm':        /* maginification */
  221.         if ((mf = atof(optarg)) > 0)
  222.             set_maginification(mf);
  223.         break;
  224.         default:
  225.         usage(argv[0], 1, 0);
  226.         }
  227.       }
  228.     return (optind < argc) ? argv[optind] : 0;
  229. }
  230.  
  231.  
  232. #include <sys/time.h>
  233.  
  234. double
  235. ReadSysClock()
  236. {
  237.     struct timeval tv;
  238.     (void) gettimeofday(&tv, (struct timezone *) NULL);
  239.     return (tv.tv_sec + tv.tv_usec / 1000000.0);
  240. }
  241.  
  242. void
  243. PrintTimeInfo()
  244. {
  245.     double spent;
  246.  
  247.     spent = ReadSysClock() - realTimeStart;
  248.  
  249.     if (!quietFlag)
  250.       {
  251.       printf("\nReal Time Spent (After Initializations): %f secs.\n", spent);
  252.       printf("Avg. Frames/Sec: %f\n", ((double) totNumFrames) / spent);
  253.       }
  254. }
  255.